package com.xcw.jac.multithreadeddownloader.threadpool;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author xcw
 * @date 2023/8/14 9:57
 * @description
 */
public class PoolTest {
    public static void main(String[] args) {
        ThreadPoolExecutor threadPoolExecutor = null;
        try {
            threadPoolExecutor = new ThreadPoolExecutor(2, 3, 1,
                    TimeUnit.MINUTES, new SynchronousQueue<>());

            Runnable r = () -> System.out.println(Thread.currentThread().getName());

            for (int i = 0; i < 5; i++) {
                threadPoolExecutor.execute(r);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (threadPoolExecutor != null) {
                threadPoolExecutor.shutdown(); // 温和
                try {
                    if (!threadPoolExecutor.awaitTermination(1, TimeUnit.MINUTES)) {

                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    threadPoolExecutor.shutdownNow(); // 暴力
                }
            }
        }
    }
}
